//
//  BBArticleViewController.m
//  BlockBrowser
//
//  Created by Mac on 2018/3/28.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "BBArticleViewController.h"
#import "BBHomeTableViewCell.h"
#import "BBHomeWebTableViewCell.h"
#import "NSCalendar+STCalendarTool.h"
#import "BBShareNewsPresentViewController.h"
#import "TMTabbarViewController.h"
#import "STPasswordTextField.h"

@interface BBArticleViewController ()
@property(nonatomic, strong) UIView                     *navView;

@end

@implementation BBArticleViewController
+ (BBArticleViewController*)defult{
    static  BBArticleViewController * vc = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        vc = [BBArticleViewController new];
    });
    return vc;
}
- (void)setDataSouce:(NSArray *)dataSouce{
    _dataSouce = dataSouce;
    [self.tableView reloadData];

}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    UIButton *btn = [self leftButton];
    self.title = @"今日文章";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.page = 1;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page  ++ ;
        [self sendHomeArtistListRequest];
    }];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    UIButton *btn = [self leftButton];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [SVProgressHUD dismiss];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)configNavView{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, [TMUtils navgationBarBootom])];
    view.backgroundColor = TM_ThemeBackGroundColor;
    
    UIButton *btn = [self leftButton];
    [view addSubview:btn];
    view.bottom = 0;
    [self.view addSubview:view];
    self.navView = view;
    

}
- (UIButton*)leftButton{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(back)  forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateHighlighted];
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    btn.frame = CGRectMake(0, 0, 44, 44);
    return btn;
}

#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1    ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [BBHomeTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    //  return 0.01;
    return 53;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 53)];
    header.backgroundColor = TM_backgroundColor;
    NSString * week = [NSCalendar st_todayWeekDay];
    NSString * title = [NSString stringWithFormat:@"今天%@   %@",[NSDate st_localCurrentDate].st_yyyyMMddhhmmssWithDate,week];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth - 30, 53)
                                                     text:title
                                                textColor:SecendTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLable];
    return header;
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    BBHomeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[BBHomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.model = self.dataSouce[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    __weak typeof(self) weakSelf =  self;
    [cell.shareButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedShareButtonWithIndexPath:indexPath];
    }];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BBArticleModel*model = self.dataSouce[indexPath.row];
    STWebViewController * vc =   [[STWebViewController alloc] initWithH5String:model.content];
    vc.forceTitle = @"文章详情";
    vc.progressBackgroundColor = FlatGray;
    vc.progressTintColor = FlatMint;
    vc.shouldNotRecord = YES;
    [self.navigationController pushViewController:vc animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)back{
    BBHomeViewController * vc = self.navigationController.childViewControllers.firstObject;
    vc.willShowResetAnimation = YES;
    [self.navigationController popViewControllerAnimated:NO];
}
- (void)onSelctedShareButtonWithIndexPath:(NSIndexPath*)indexPath{
    BBArticleModel *  model = self.dataSouce[indexPath.row];
    BBHomeTableViewCell * cell = [(id)self.tableView cellForRowAtIndexPath:indexPath];
    BBShareNewsPresentViewController * vc = [BBShareNewsPresentViewController new];
    vc.time = model.dateline;
    vc.news = cell.contenLable.text;
    [vc setShareButtonHandle:^(NSString *title, UIImage *shareImage) {
        TMTabbarViewController * tabvc = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
        [tabvc shareWithChosedTitle:title image:shareImage];
    }];;
    [self presentViewController:vc animated:NO completion:nil];
}
#pragma mark --NetWork Method
- (void)sendHomeArtistListRequest{
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",severHeader,@"/api/news/list"];
    [dic setObject:@(_page) forKey:@"page"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypeGet
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSArray * array = responseObject[@"data"][@"list"];
                                                 NSMutableArray * dataSouce = [self.dataSouce mutableCopy];
                                                 for (NSDictionary * modelDic in array) {
                                                     BBArticleModel * model = [BBArticleModel mj_objectWithKeyValues:modelDic];
                                                     [dataSouce addObject:model];
                                                 }
                                                 self.dataSouce = [dataSouce copy];
                                              //   [self.tableView.mj_footer endRefreshing];
                                                 [self.tableView reloadData];

                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                             }];
}
@end
